package com.wrk._3_方法._02_习题;

import java.util.Arrays;
import java.util.Scanner;

public class _007_数据加解密 {
    /**规则如下：先得到每位数，然后每位数都加上5，再对10求余，最后将所有数字反转，得到一串新数。
    1、先得到数字密码(大于0)，设置成数组
    2、对每一位元素+5
    3、除10取余
    4、进行反转*/
    public static void main(String[] args) {
        //获取密码
        Scanner scanner = new Scanner(System.in);
        System.out.print("前请输入密码：");
        int passwd = scanner.nextInt();
        //计算密码长度
        int passwdLength = String.valueOf(passwd).length();

        /**加密结果：*/
            //1、给原始字符串密码转换为数组方法  赋值
        int[] passwdArray = getInts(passwd, passwdLength);
            //2、调用加密方法，赋值
        int[] array6 = passwdEncryption(passwdArray);
             //3、加密后密码数组转换成字符串，输出
        //StringBuilder sb1 = convertString(array6);
        int sb1 = convert(array6);
        System.out.println("加密后密码为："+sb1);

        /**解密结果：*/
            //1、加密后密码转换为int类型
        //passwd = Integer.parseInt(sb1.toString());
            //2、给加密后字符串密码转换为数组  赋值
        int[] passwdArray2 = getInts(sb1, passwdLength);
            //2、调用解密方法，赋值
        int [] decryptArray = passwdDecrypt(passwdArray2);
            //3、解密后密码数组转换成字符串，输出
        //StringBuilder sb2 = convertString(decryptArray);
        int sb2 = convert(decryptArray);
        System.out.println("解密后密码为："+sb2);

    }
    /**字符串密码转换为数组
     * @param passwd 密码
     * @param passwdLength 密码长度*/
    private static int[] getInts(int passwd, int passwdLength) {
        int[] originalaArray = new int[passwdLength];
        for (int i = 0; i < originalaArray.length; i++) {
            if (passwd % 10 > 0){
                originalaArray[originalaArray.length-1-i] = passwd % 10;
                passwd =  passwd / 10;
            }
        }
        return originalaArray;
    }

    /**加密方法
     * @param array 未加密时的数组*/
    public static int[] passwdEncryption(int [] array ){
        //每个元素+5
        for (int i = 0; i < array.length; i++) {
            array[i] += 5;
        }
        //每个元素除10，取余
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] % 10;
        }
        //反转数组
        int a =0;
        for (int i = 0; i < array.length/2; i++) {
            a = array[i];
            array[i] = array[array.length -(i+1)];
            array[array.length -(i+1)] = a ;
        }
        return array;
    }

    /**解密方法
     * @param arrays 加密后，转换的数组*/
    public static int [] passwdDecrypt(int [] arrays){
        //反转数组
        int b =0;
        for (int i = 0; i < arrays.length/2; i++) {
            b = arrays[i];
            arrays[i] = arrays[arrays.length-(i+1)];
            arrays[arrays.length-(i+1)] = b;
        }
        //反向推导解密
        for (int i = 0; i < arrays.length; i++) {
            //元素在5-9之间的，+5除10取余
            if (arrays[i] >= 5 && arrays[i] <=9){
                arrays[i] = (arrays[i] + 5) % 10;
            }
            //元素在0-4之间的，+5
            else {
                arrays[i] = arrays[i] + 5;
            }
        }
        return arrays;
    }

    /**数组转换成字符串*/
    public static int convert(int [] array6){
        //StringBuilder sb = new StringBuilder();
        int passwdConvert= 0;
        for (int i = 0; i < array6.length; i++) {
            //sb.append(array6[i]);
            passwdConvert = (passwdConvert * 10) + array6[i];
        }
        return passwdConvert;
    }
}
